#This R code creates Figures 1-2 in the main paper

#clear memory
rm(list=ls())

#set random number seed
set.seed(123456789)

#load necessary R packages
library(data.table)
library(lubridate)
library(MASS)
library(foreign)
library(lfe)
library(lmtest)
library(texreg)
library(methods)
library(doBy)
library(ggplot2)
library(TTR)
library(stringr)

#set working directory
setwd("")  

###############
###Figure 2####
###############

#load-in Data
fulltemplate<-fread("TimeSeries.ForPlotting.csv",encoding="UTF-8")

#format data
fulltemplate$date<-paste(fulltemplate$month,fulltemplate$day,fulltemplate$year,sep="/")
fulltemplate$date<-as.Date(fulltemplate$date, "%m/%d/%Y")

#assign column names
colnames(fulltemplate)<-c("TopicAll","Topic1","Topic2","Topic3","Topic4","Topic5","Topic6","Topic7",  
   "Topic8","Topic9","Topic10","Topic11","Topic12","Topic13","Topic14","Topic15", 
   "Topic16","Topic17","Topic18","Topic19","Topic20","edate","year","month","day","date")

#set up plot for saving
png("TopicTimeplot.png", units="in", width=10.64,height=6.24,  res=300)

#make actual plot
ggplot(fulltemplate, aes(x=date)) + 
  stat_smooth(aes(y = Topic1, color = "Topic1"),se = FALSE) + 
  stat_smooth(aes(y = Topic2, color="Topic2"),se = FALSE) + 
  stat_smooth(aes(y = Topic3, color = "Topic3"),se = FALSE) + 
  stat_smooth(aes(y = Topic4, color="Topic4"),se = FALSE) + 
  stat_smooth(aes(y = Topic5, color = "Topic5"),se = FALSE) + 
  stat_smooth(aes(y = Topic6, color="Topic6"),se = FALSE) + 
  stat_smooth(aes(y = Topic7, color = "Topic7"),se = FALSE) + 
  stat_smooth(aes(y = Topic8, color="Topic8"),se = FALSE) + 
  stat_smooth(aes(y = Topic9, color = "Topic9"),se = FALSE) + 
  stat_smooth(aes(y = Topic10, color="Topic10"),se = FALSE) + 
  stat_smooth(aes(y = Topic11, color = "Topic11"),se = FALSE) + 
  stat_smooth(aes(y = Topic12, color="Topic12"),se = FALSE) + 
  stat_smooth(aes(y = Topic13, color = "Topic13"),se = FALSE) + 
  stat_smooth(aes(y = Topic14, color="Topic14"),se = FALSE) + 
  stat_smooth(aes(y = Topic15, color = "Topic15"),se = FALSE) + 
  stat_smooth(aes(y = Topic16, color="Topic16"),se = FALSE) + 
  stat_smooth(aes(y = Topic17, color = "Topic17"),se = FALSE) + 
  stat_smooth(aes(y = Topic18, color="Topic18"),se = FALSE) + 
  stat_smooth(aes(y = Topic19, color = "Topic19"),se = FALSE) + 
  stat_smooth(aes(y = Topic20, color="Topic20"),se = FALSE) +
  scale_color_manual(name = "Topic", values = c("Topic1" = "darkred", "Topic2" = "steelblue", "Topic3" = "blue",
                "Topic4" = "magenta", "Topic5" = "lightblue", "Topic6" = "yellow","Topic7" = "darkseagreen1", "Topic8" = "darkorange",
                "Topic9" = "black", "Topic10" = "red", "Topic11" = "aquamarine","Topic12" = "coral4", "Topic13" = "purple",
                 "Topic14" = "cyan4", "Topic15" = "deeppink", "Topic16" = "darkgoldenrod","Topic17" = "brown4", "Topic18" = "brown1",
                "Topic19" = "cyan3",      "Topic20" = "darkolivegreen1"),breaks=c('Topic1','Topic2','Topic3','Topic4','Topic5','Topic6','Topic7','Topic8',
                'Topic9','Topic10','Topic11','Topic12','Topic13','Topic14','Topic15','Topic16','Topic17','Topic18','Topic20'))+labs(y= "Request (Daily Frequency)", x = "Date") +   
geom_vline(xintercept = as.numeric(as.Date("2020-06-30")),color = "red", linetype = "dashed", size = 1) +      
geom_vline(xintercept = as.numeric(as.Date("2020-04-01")),color = "blue", linetype = "dashed", size = 1) +
annotate("text", x=as.Date("2020-10-05"), y=0.33,color = "red",  label= "NUS") +
annotate("text", x=as.Date("2019-10-20"), y=0.33,color = "blue",  label= "CivicSight") + scale_y_continuous(breaks = c(0.0,0.1,0.2,0.3), 
                     labels = c(0,2,4,6))
#outwrite
dev.off()


###############
###Figure 1####
###############

#read in full dataset
fulldataset<-read.csv("Final.Dataset.csv")

#format data
fulldataset$year<-ifelse(is.na(fulldataset$year),str_sub(fulldataset$date,-4,-1),fulldataset$year)
fulldataset$day<-ifelse(is.na(fulldataset$day),str_sub(fulldataset$date,1,2),fulldataset$day)
fulldataset$month<-ifelse(is.na(fulldataset$month) & str_sub(fulldataset$date,3,5)=="apr",4,fulldataset$month)
fulldataset$month<-ifelse(is.na(fulldataset$month) & str_sub(fulldataset$date,3,5)=="may",5,fulldataset$month)

#collapse
responsedata <- summaryBy(deny+partial+success ~ year+month+day, FUN=c(function(x)sum(x, na.rm=TRUE)), data=fulldataset)

#fix day and month
responsedata$day<-as.character(responsedata$day)
responsedata$month<-as.character(responsedata$month)
responsedata$day<-ifelse(nchar(responsedata$day)==1,paste("0",responsedata$day,sep=""),responsedata$day)
responsedata$month<-ifelse(nchar(responsedata$month)==1,paste("0",responsedata$month,sep=""),responsedata$month)
responsedata$date<-paste(responsedata$month,responsedata$day,responsedata$year,sep="/")
responsedata$date<-as.Date(responsedata$date, "%m/%d/%Y")

#rename columns and create additional measure
colnames(responsedata)<-c("year","month", "day","Denied","Partial","Successful", "date") 
responsedata$all<-responsedata$Denied+responsedata$Partial+responsedata$Supplied
summary(responsedata)

#set up plot for saving
png("ResponseTimeplot.png", units="in", width=10.64,height=6.24,  res=300)

#generate plot
ggplot(responsedata, aes(x=date)) + 
  stat_smooth(aes(y = Denied, color = "Denied"),se = FALSE) + 
  stat_smooth(aes(y = Partial, color="Partial"),se = FALSE) + 
  stat_smooth(aes(y = Successful, color = "Successful"),se = FALSE) + 
  scale_color_manual(name = "Response", values = c("Denied" = "darkred", "Partial" = "steelblue", "Successful" = "blue"),breaks=c('Denied','Partial','Successful'))+labs(y= "Request (Daily Frequency)", x = "Date") +   
geom_vline(xintercept = as.numeric(as.Date("2020-06-30")),color = "red", linetype = "dashed", size = 1) +      
geom_vline(xintercept = as.numeric(as.Date("2020-04-01")),color = "blue", linetype = "dashed", size = 1) +
annotate("text", x=as.Date("2020-10-05"), y=0.83,color = "red",  label= "NUS") +
annotate("text", x=as.Date("2019-10-20"), y=0.83,color = "blue",  label= "CivicSight") + scale_y_continuous(breaks = c(0.2,0.4,0.6,0.8),                     labels = c(4,8,12,16))

#outwrite
dev.off()
